import 'phaser'

export default class GameScene extends Phaser.Scene {
    constructor() {
        super({
            key: 'GameScene'
        })
        var bg;
        var iter = 0
    }

    preload() {

    }

    create() {
        this.bg = this.add.tileSprite(347 / 2, 570 / 2, this.sys.game.config.width / 1,
            this.sys.game.config.height / 1, 'background');
        var dull = this.add
            .text(10, 10, "Score：",
                {fontFamily: "Arial Black", fontSize: 16, color: "#c51b7d"})
        this.hero = this.physics.add.sprite(347 / 2, 570 / 2, 'hero');
        this.hero.setCollideWorldBounds(true);
        this.tweens.add({
            targets: this.hero,
            y: 520,
        });
        this.cursors = this.input.keyboard.createCursorKeys();
        this.bullets = this.physics.add.group();
        this.bulletTime = 0;
        this.enemy = this.physics.add.sprite(347 / 2, 60, 'enemy');
        this.enemy.scale = 0.68;
        this.enemy.rotation = -45
        this.physics.add.overlap(this.enemy, this.bullets, this.hitEnemy, null, this);
    }

    update() {
        let velocityX = 260;
        let VelocityY = 280;
        this.bg.tilePositionY += -2;
        if (this.cursors.left.isDown) {
            this.hero.setVelocityX(-velocityX);
        } else if (this.cursors.right.isDown) {
            this.hero.setVelocityX(velocityX);
        } else {
            this.hero.setVelocityX(0);
        }
        if (this.cursors.up.isDown) {
            this.hero.setVelocityY(-VelocityY);
        } else if (this.cursors.down.isDown) {
            this.hero.setVelocityY(VelocityY);
        } else {
            this.hero.setVelocityY(0);
        }
        // this.heroBullet.body.velocity.y = -180;
        let nowTime = new Date().getTime();
        if (nowTime - this.bulletTime > 300) {
            this.heroBullet = this.bullets.create(this.hero.x, this.hero.y - 30, 'bullet');
            this.heroBullet.setVelocityY(-200);
            this.bulletTime = nowTime;
        }
    }

    hitEnemy(enemy, bullet) {
        // console.log(arguments)
        enemy.disableBody(true, true);
        bullet.disableBody(true, true);
    }
}
